import { defineStore } from "pinia";
import type { IMenu } from "@/types/global";
interface IUserInfoStore {
	menus: Array<IMenu>;
	username: string;
}
const useUserInfoStore = defineStore("useUserInfoStore", {
	state: (): IUserInfoStore => {
		return {
			menus: [],
			username: "",
		};
	},
	getters: {
		filterMenus: (state) => {
			let menus = state.menus;
			menus.sort((a, b) => a.parentId - b.parentId);
			menus.forEach((el) => {
				// if (el.hidden === 0) {
					if (el.parentId === 0) {
						el.children = [];
					}
					const index = menus.findIndex((item) => el.parentId === item.id);
					if (index > -1) {
						menus[index].children?.push(el);
					}
				// }
			});
			menus = menus.filter((item) => item.parentId === 0);
			// console.log(menus);
			return menus;
		},
	},
	actions: {
		updateUserInfo(val: Array<IMenu>, username: string) {
			this.menus = val;
			this.username = username;
		},
	},
});

export default useUserInfoStore;
